﻿using System.Configuration;

namespace Isaac.Joy.Ormapping.Contract
{

    public class DatabaseProviderFactory: IDatabaseProviderFactory
    {
        public const string SectionName = "dataConfiguration";
        public const string ConnectionSectionName = "connectionStrings";
        public IDatabase CreateDefault()
        {
            var setting = (DatabaseSettings)ConfigurationManager.GetSection(SectionName);
            return CreateDatabase(setting.DefaultDatabase);
        }

        public IDatabase CreateDatabase(string name)
        {
            var cns = (ConnectionStringsSection)ConfigurationManager.GetSection(ConnectionSectionName);
            if (cns.ConnectionStrings.Count <= 0) return null;

            var cn = string.IsNullOrEmpty(name) ? cns.ConnectionStrings[0] : cns.ConnectionStrings[name];
            return CreateDatabase(cn.ConnectionString, cn.ProviderName);
        }

        private static IDatabase CreateDatabase(string cnstr, string provider)
        {
            return null;
//            switch (provider)
//            {
//                case "System.Data.SqlClient":
//                    return new Database<SqlConnection>(cnstr);
//                case "System.Data.OracleClient":
//#pragma warning disable 618
//                    return new Database<OracleConnection>(cnstr);
//#pragma warning restore 618
//                default:
//                    return null;
//            }
        }
    }
}
